using System;
using System.Configuration;
using System.Globalization;
using System.Threading;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using SysOperationLayer.DBHandle;
using Resources;

public partial class OptRespPerson : Page, IRequiresSessionState
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            UsersDB objDB = new UsersDB();
            ddlRespPerson.DataSource = objDB.GetResponsiblePerson("");
            ddlRespPerson.DataTextField = "UserName";
            ddlRespPerson.DataValueField = "LoginName";
            ddlRespPerson.DataBind();

            gvRespPersons.DataSource = new ProjectDB(objDB.GetDB()).GetResponsiblePerson(Session["ProjectCode"].ToString());
            gvRespPersons.DataBind();

            objDB.CloseDB();

            btnDelete.Attributes.Add("onClick", "return confirm('" + Messages.DeleteConfirmation.ToString() + "')");
        }

        setTextReadOnly(true);
        setButtons(string.Empty);
    }

    protected override void InitializeCulture()
    {
        string strCulture = ConfigurationManager.AppSettings["DefaultCulture"].ToString();

        try
        {
            if (Session["Culture"] != null)
            {
                strCulture = Session["Culture"].ToString();
            }
        }
        catch (NullReferenceException)
        {
        }

        Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(strCulture);
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(strCulture);
        base.InitializeCulture();
    }

    protected void OnClick(object sender, EventArgs e)
    {
        if (sender == btnNew)
        {
            clearText();
            setTextReadOnly(false);
            setButtons("new");
            ddlRespPerson.DataBind();
            ddlRespPerson.Focus();
        }
        else if (sender == btnModify)
        {
            lblError.Text = "Update is not allowed on primary keys. ";
        }
        else if (sender == btnSave)
        {
            string projectCode = Session["ProjectCode"].ToString();
            string ownerGroupID = Session["OwnerGroupID"].ToString();
            ProjectDB objDB = new ProjectDB();

            if (objDB.InsertResponsiblePerson(projectCode, ddlRespPerson.SelectedValue.ToString(), ownerGroupID, Page.User.Identity.Name.ToString(), HttpContext.Current.Request.UserHostAddress) == 0)
            {
                lblError.Text = "Update Success!";
                gvRespPersons.DataSource = objDB.GetResponsiblePerson(projectCode);
                gvRespPersons.DataBind();
                OnClick(btnCancel, null);
            }
            else
            {
                lblError.Text = "Update Failure: " + objDB.ErrorMessage;
            }

            objDB.CloseDB();
        }
        else if (sender == btnDelete)
        {
            string projectCode = Session["ProjectCode"].ToString();
            ProjectDB objDB = new ProjectDB();

            if (objDB.DeleteResponsiblePerson(projectCode, ddlRespPerson.SelectedValue.ToString(), Page.User.Identity.Name.ToString(), HttpContext.Current.Request.UserHostAddress) == 0)
            {
                lblError.Text = "Delete Record Success!";
                gvRespPersons.DataSource = objDB.GetResponsiblePerson(projectCode);
                gvRespPersons.DataBind();
                OnClick(btnCancel, null);
            }
            else
            {
                lblError.Text = "Delete Record Failure: " + objDB.ErrorMessage;
            }

            objDB.CloseDB();
        }
        else if (sender == btnCancel)
        {
            clearText();
            setTextReadOnly(true);
            setButtons(string.Empty);
        }
    }

    protected void clearText()
    {
        lblError.Text = "";
        ddlRespPerson.SelectedIndex = -1;
    }

    protected void OnRowCreated(object sender, GridViewRowEventArgs e)
    {
        Sort(e, gvRespPersons);
    }

    protected void OnSelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            ddlRespPerson.SelectedIndex = -1;
            ddlRespPerson.Items.FindByValue(gvRespPersons.DataKeys[gvRespPersons.SelectedIndex].Values["ResponsiblePerson"].ToString()).Selected = true;
        }
        catch (NullReferenceException)
        {
        }
        catch (ArgumentOutOfRangeException)
        {
        }
    }

    public void setButtons(string action)
    {
        switch (action)
        {
            case "new":
                btnNew.Enabled = false;
                btnModify.Enabled = false;
                btnSave.Enabled = true;
                btnDelete.Enabled = false;
                return;

            case "modify":
                btnNew.Enabled = false;
                btnModify.Enabled = false;
                btnSave.Enabled = true;
                btnDelete.Enabled = false;
                return;
        }
        btnNew.Enabled = true;
        btnModify.Enabled = !ddlRespPerson.Enabled;
        btnSave.Enabled = false;
        btnDelete.Enabled = !ddlRespPerson.Enabled;
    }

    protected void setTextReadOnly(bool bBool)
    {
        ddlRespPerson.Enabled = !bBool;
    }

    protected void Sort(GridViewRowEventArgs e, GridView gv)
    {
        if ((e.Row != null) && (e.Row.RowType == DataControlRowType.Header))
        {
            foreach (TableCell cell in e.Row.Cells)
            {
                if (cell.HasControls())
                {
                    LinkButton button = (LinkButton)cell.Controls[0];
                    if (button != null)
                    {
                        Image image = new Image();
                        image.ImageUrl = "";
                        if (gv.SortExpression == button.CommandArgument)
                        {
                            if (gv.SortDirection == SortDirection.Ascending)
                            {
                                image.ImageUrl = "images/sortup.gif";
                            }
                            else
                            {
                                image.ImageUrl = "images/sortdown.gif";
                            }
                        }
                        if (image.ImageUrl != "")
                        {
                            cell.Controls.Add(image);
                        }
                    }
                }
            }
        }
    }
}
